<?php

namespace Home\Model;

require_once dirname(__FILE__).'/../../../../vendor/workerman/gatewayclient/Gateway.php';

use GatewayClient\Gateway;
use Think\Model;

class TaskModel extends CommonModel
{
    public function finish($task, $user_id, $value='')
    {
        $TaskModel = new TaskModel;
        $UserTaskModel = new UserTaskModel;
        
        $task = $TaskModel->where(['name' => $task, 'is_hidden' => 0, 'channel' => 1])->find();

        if (!$task) {
            return false;
        }

        $userTask = $UserTaskModel->where(['task' => $task['name'], 'user_id' => $user_id, 'channel' => 1])->find();

        if ($task['types'] == 1 && $userTask) {
            return false;
        }

        if ($task['value'] && $task['value'] >= $value) {
            return false;
        }

        if ($task['types'] == 2 && $task['max']) {
            $start = strtotime(date('Ymd'));
            //$cost = (new VolleyDescModel)->where(['user_id' => $user_id, 'content' => '[完成任务] ' . $task['title'], 'time' => ['egt', $start]])->sum('price');
            $cost = (new TaskDailyModel)->where(['user_id' => $user_id, 'name' => $task['name'], 'create_time' => ['egt', $start], 'channel' => 1])->sum('reward');

            if (($cost + $task['reward']) > $task['max']) {
                return false;
            }
        }
    
        $UserTaskModel->add(['task' => $task['name'], 'user_id' => $user_id, 'create_time' => time(), 'channel' => 1]);

        if ($task['reward'] + 0) {
            (new VolleyDescModel)->index($user_id, $task['reward'], '[完成任务] ' . $task['title'], 1);
    
            if ($task['types'] == 2 && $task['max']) {
                $TaskModel->updateTaskDaily($user_id, $task['name'], $task['reward']);
            }

            $message = json_encode([
                "code" => 200,
                "action" => 'model/task/finish',
                'data' => [
                    "title" => $task['title'] . '奖励红包',
                    "money" => $task['reward'],
                    'msg' => '已存入您的账户，可提现哦~'
                ]
            ], JSON_UNESCAPED_UNICODE);

            Gateway::sendToUid(getenv('SOCKET_PREFIX') . $user_id, $message);
        }
    }
    
    public function updateTaskDaily($user_id, $task, $reward)
    {
        $TaskDailyModel = new TaskDailyModel;
        
        if ($reward > 0) {
            $taskDaily = $TaskDailyModel->where(['user_id' => $user_id, 'name' => $task, 'channel' => 1])->find();
            
            if ($taskDaily) {
                $today_start = strtotime(date('Y-m-d'));

                if ($taskDaily['create_time'] < $today_start) {
                    $TaskDailyModel->save([
                        'id' => $taskDaily['id'],
                        'reward' => $reward,
                        'create_time' => time(),
                    ]);
                } else {
                    $TaskDailyModel->save([
                        'id' => $taskDaily['id'],
                        'reward' => $reward + $taskDaily['reward'],
                        'create_time' => time(),
                    ]);
                }
            } else {
                $TaskDailyModel->add([
                    'user_id' => $user_id,
                    'name' => $task,
                    'reward' => $reward,
                    'create_time' => time(),
                    'channel' => 1
                ]);
            }
        }
    }
}
